#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"

#include"lwip/netifapi.h"
#include "lwip/api_shell.h"
#include "lwip/sockets.h"
#include "WifiSta.h"

#include "Mpu6050.h"
#include "Led.h"

#define SSID "Nice"
#define Key "12312312"
#define TCP_IP "192.168.101.231"
#define TCP_PORT 5552
#define SENDMSG_HEAD "C"

#define FLIP_THRESHOLD 100
#define TASK_DELAY 3
#define SENSOR_TASK_STACK_SIZE (1024 * 4)
#define SENSOR_TASK_PRIO 25

int g_coverStatus = 0;
char sendBuf[256] = {0};

int ManholeModuleTask(void);
int TASK2(void);


static void ManholeSample(void)
{
    /* Manhole 模块初始化 */
	ManholeLedInit();
    
	/* 设置线程属性 */
    osThreadAttr_t attr;
    attr.name = "ManholeModuleTask";
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = SENSOR_TASK_STACK_SIZE;
    attr.priority = SENSOR_TASK_PRIO;
    /* 创建智慧井盖检测任务 */
    osThreadNew((osThreadFunc_t)ManholeModuleTask, NULL, &attr);
    attr.name = "Task2";
    osThreadNew(TASK2,NULL,&attr);
}
int ManholeModuleTask(void)
{
    uint8_t ret;
    MPU6050Data data;
    int X = 0, Y = 0, Z = 0;

    ret = ManholeInit();
    if (ret != 0) {
        printf("ManholeInit failed!\r\n");
        return 0;
    }
    sleep(5);
    while (1) {
        ret = ManholeReadData(&data);
        if (ret != 0) {
            printf("E53_SC2 Read Data!\r\n");
            return 0;
        }
        // printf("\r\nTemperature      is  %d\r\n", (int)data.Temperature);
        // printf("Accel[ACCEL_X_AXIS] is  %d\r\n", (int)data.Accel[ACCEL_X_AXIS]);
        // printf("Accel[ACCEL_Y_AXIS] is  %d\r\n", (int)data.Accel[ACCEL_Y_AXIS]);
        // printf("Accel[ACCEL_Z_AXIS] is  %d\r\n", (int)data.Accel[ACCEL_Z_AXIS]);
        if (X == 0 && Y == 0 && Z == 0) {
            X = (int)data.Accel[ACCEL_X_AXIS];
            Y = (int)data.Accel[ACCEL_Y_AXIS];
            Z = (int)data.Accel[ACCEL_Z_AXIS];
        } else {
            if (X + FLIP_THRESHOLD < data.Accel[ACCEL_X_AXIS] || X - FLIP_THRESHOLD > data.Accel[ACCEL_X_AXIS] ||
                Y + FLIP_THRESHOLD < data.Accel[ACCEL_Y_AXIS] || Y - FLIP_THRESHOLD > data.Accel[ACCEL_Y_AXIS] ||
                Z + FLIP_THRESHOLD < data.Accel[ACCEL_Z_AXIS] || Z - FLIP_THRESHOLD > data.Accel[ACCEL_Z_AXIS]) {
                ManholeLedCtrl(LED_ON);
                printf("Manhole cover flipped, LED ON!\r\n");
                g_coverStatus = 1;
            } else {
                ManholeLedCtrl(LED_OFF);
                printf("Manhole cover restored, LED OFF!\r\n");
                g_coverStatus = 0;
            }
        }

        //控制检测频率
        sleep(TASK_DELAY);
    }
    return 0;
}
int TASK2(void){
    struct sockaddr_in send_addr={0};
    //WIFI 连接
    ip4_addr_t ip = WifiConnect(SSID,Key);
    sleep(2);

    //创建socket
    int socketfd = socket(AF_INET,SOCK_STREAM,0);
    if(socketfd == -1)
    {
        printf("socket create failed\n");
        return -1;
    }
    printf("socket create ok\n");

    send_addr.sin_family = AF_INET;
    send_addr.sin_addr.s_addr = inet_addr(TCP_IP);
    send_addr.sin_port = htons(TCP_PORT);
    int ret = connect(socketfd,(struct sockaddr *)&send_addr,sizeof(send_addr));
    if (ret != 0)
    {
        printf("socket connect failed is %d\n",ret);
        closesocket(socketfd);
        return -1;
    }
    printf("socket connect ok\n");
    //连接服务器后发送和接收数据
    char str[20] = {0};
    int count = 0;
    while (1)
    {
      memset(sendBuf,0,sizeof(sendBuf));
      if(count < 1){//发送IP
        strcpy(sendBuf, SENDMSG_HEAD);  
        sprintf(str, "%d",ip);
        strcat(sendBuf,str);
        count ++;
      }else{
        strcpy(sendBuf, str);
        if(g_coverStatus == 0) strcat(sendBuf,"OFF");
        else if(g_coverStatus == 1) strcat(sendBuf,"ON");
      }
      ret = send(socketfd,sendBuf,strlen(sendBuf)+1,0);
      if (ret == -1)
        {
            printf("socket send failed is %d\n",ret);
            break;
        }
        printf("socket send ok\n");
        sleep(2);
    }
    closesocket(socketfd);
    return 0;
}
APP_FEATURE_INIT(ManholeSample);